﻿using System;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace RestAPI
{
    public static partial class RestAPICommon
    {
        #region 常量

        public const string WebCallCmd = "webcall";
        public const string WebCallToneCmd = "webcalltone";
        public const string WebCallTextCmd = "webcalltext";
        public const string WebCallVerificationcodeCmd = "webcallverificationcode";
        public const string WebCallIvrCmd = "webcallivr";
        public const string WebCallGroupCmd = "webcallgroup";
        public const string WebCallGrouptoneCmd = "webcallgrouptone";
        public const string WebCallWithcollectinfoCmd = "webcallwithcollectinfo";

        #endregion


        /// <summary>
        /// 普通呼叫 
        /// </summary>
        /// <param name="pDic"></param>
        /// <param name="pPostUrl"></param>
        /// <returns></returns>
        public static WebRequestError WebCall(Dictionary<string, string> pDic, string pPostUrl, GetPostResponseHandler ResponseHandler = null)
        {
            if (pDic.Count == 0)
            {
                throw new Exception("请求参数为空。");
            }
            string read = string.Empty;
            return WebClientPost(ServerPath + "/" + Services + "/" + pPostUrl, GetParamWithDigset(pDic), ResponseHandler);
        }

        #region 过时的

        ///// <summary>
        ///// 可信语音外呼
        ///// </summary>
        ///// <param name="pDicParam"></param>
        ///// URL ：http://42.121.28.96:8080/zwgw/webcalltone
        ///// <returns></returns>
        //public static WebRequestError WebCallToneFun()
        //{
        //    WebCalltone model = new WebCalltone
        //    {
        //        Companyid = "101",
        //        CallFlowid = "111",
        //        Calling = "222",
        //        Called = "333",
        //        Callback = "http://",
        //        Callmode = "0",
        //        Toneid = "222",
        //        Playtime = "200",
        //    };
        //    Dictionary<string, string> dicParam = CommonFun.GetDictionaryByModel(model);
        //    return RestAPI.RestAPICommon.WebCall(dicParam, "webcalltone");
        //}
        ///// <summary>
        ///// 可信文本外呼
        ///// http://42.121.28.96:8080/zwgw/webcalltext
        ///// </summary>
        ///// <returns></returns>
        //public static WebRequestError WebCallText()
        //{
        //    RestAPIModel.WebCallText model = new WebCallText
        //    {
        //        Companyid = "101",
        //        CallFlowid = "111",
        //        Calling = "222",
        //        Called = "333",
        //        Callback = "http://",
        //        Callmode = "0",
        //        PlayTime = "1",
        //        Voice_Text = ""
        //    };
        //    Dictionary<string, string> dicParam = CommonFun.GetDictionaryByModel(model);
        //    return CommonFun.WebClientPost("webcalltext", CommonFun.GetDigset(dicParam));

        //}
        ///// <summary>
        ///// 语音验证码
        ///// </summary>
        ///// <returns></returns>
        //public static WebRequestError WebCallVerifiCationCodeFun()
        //{
        //    WebCallVerifiCationCode model = new WebCallVerifiCationCode
        //    {
        //        Companyid = "101",
        //        Callback = "http://",
        //        CallFlowid = "111",
        //        Calling = "122",
        //        Voice_Text = "test"
        //    };

        //    Dictionary<string, string> dicParam = CommonFun.GetDictionaryByModel(model);
        //    return CommonFun.WebClientPost("webcallverificationcode", CommonFun.GetDigset(dicParam));

        //}
        /// <summary>
        /// 普通外呼IVR
        /// </summary>
        /// <returns></returns>
        //public static WebRequestError WebCallIVR()
        //{

        //    WebCallIvr model = new WebCallIvr
        //    {
        //        Companyid = "101",
        //        CallFlowid = "111",
        //        Calling = "122",
        //        CallBack = "http://",
        //        IvrID = "44",
        //        Voice_Text = "text"
        //    };
        //    Dictionary<string, string> dicParam = CommonFun.GetDictionaryByModel(model);
        //    return CommonFun.WebClientPost("webcallivr", CommonFun.GetDigset(dicParam));

        //}

        #endregion

        /// <summary>
        /// 坐席组外呼
        /// http://42.121.28.96:8080/zwgw/webcallgroup
        /// </summary>
        /// <returns></returns>
        public static WebRequestError WebCallGroup()
        {
            return WebRequestError.服务暂停_503;

        }

        /// <summary>
        /// 呼叫扩展方法
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="model"></param>
        /// <returns></returns>
        public static WebRequestError Call<T>(this T model, GetPostResponseHandler ResponseHandler = null) where T : RestAPIModelBase
        {
            try
            {
                string webCallCmd = "";

                if ((model as WebCall) != null)
                    webCallCmd = WebCallType.webcall.ToString();

                else if ((model as WebCalltone) != null)
                    webCallCmd = WebCallType.webcalltone.ToString();

                else if ((model as WebCallText) != null)
                    webCallCmd = WebCallType.webcalltext.ToString();

                else if ((model as WebCallVerifiCationCode) != null)
                    webCallCmd = WebCallType.webcallverificationcode.ToString();

                else if ((model as WebCallIvr) != null)
                    webCallCmd = WebCallType.webcallivr.ToString();

                else if ((model as WebCallGroup) != null)
                    webCallCmd = WebCallType.webcallgroup.ToString();

                else if ((model as WebCallGroupTone) != null)
                    webCallCmd = WebCallType.webcallgrouptone.ToString();

                else if ((model as WebCallWithcollectinfo) != null)
                    webCallCmd = WebCallType.webcallwithcollectinfo.ToString();

                else if (model as WebVideoCall != null)
                    webCallCmd = WebVideoCallType.webvideocall.ToString();

                else if (model as WebVideoConfCall != null)
                    webCallCmd = WebVideoCallType.confcall.ToString();

                else if (model as WebVideoConfCtrlAdd != null)
                    webCallCmd = WebVideoCallType.confctrladd.ToString();

                else if (model as WebVideoConfCtrlDel != null)
                    webCallCmd = WebVideoCallType.confctrldel.ToString();

                else if (model as WebVideoConfCtrlHalt != null)
                    webCallCmd = WebVideoCallType.confctrlhalt.ToString();

                else if (model as WebVideoConfCtrlView != null)
                    webCallCmd = WebVideoCallType.confctrlview.ToString();

                else if (model as WebVideoConfInfo != null)
                    webCallCmd = WebVideoCallType.getconfinfo.ToString();

                else if (model as WebVideoConfMemberInfo != null)
                    webCallCmd = WebVideoCallType.getconfmemberinfo.ToString();

                else if (model as MChangepwd != null)
                    webCallCmd = ManagerMentType.changepwd.ToString();

                else if (model as MCreatedevice != null)
                    webCallCmd = ManagerMentType.createdevice.ToString();

                else if (model as MGetaccountfrommac != null)
                    webCallCmd = ManagerMentType.getaccountfrommac.ToString();

                else if (model as MGetagentstatus != null)
                    webCallCmd = ManagerMentType.getagentstatus.ToString();

                else if (model as MGetgroup != null)
                    webCallCmd = ManagerMentType.getgroup.ToString();

                else if (model as MGetgroupmember != null)
                    webCallCmd = ManagerMentType.getgroupmember.ToString();

                else if (model as MGetgwstatus != null)
                    webCallCmd = ManagerMentType.getgwstatus.ToString();

                else if (model as MQuerycompanyinfo != null)
                    webCallCmd = ManagerMentType.querycompanyinfo.ToString();

                else if (model as MSetphonestatus != null)
                    webCallCmd = ManagerMentType.setphonestatus.ToString();

                else if (model as MSSO != null)
                    webCallCmd = ManagerMentType.sso.ToString();

                else if (model as MTTS != null)
                    webCallCmd = ManagerMentType.tts.ToString();

                else if (model as Mzwangetvideopath != null)
                    webCallCmd = ManagerMentType.zwangetvideopath.ToString();

                else if (model as WebVideoConfMsg != null)
                    webCallCmd = WebVideoCallType.confmsg.ToString();

                else if (model as WebVideoConfScreen != null)
                    webCallCmd = WebVideoCallType.confscreen.ToString();

                else if (model as WebVideoConfsilence != null)
                    webCallCmd = WebVideoCallType.confsilence.ToString();


                if (webCallCmd == "")
                    return WebRequestError.类型错误;


                return WebCall(model.GetDictionaryByModel(), webCallCmd, ResponseHandler);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Post error : " + model.ToString() + "\r\n" + ex.Message);
                return WebRequestError.类型错误;
            }
        }

        public async static void CallBackAsync<T>(this T model, GetPostResponseHandler cbHandler, int delay = 1000) where T : RestAPIModelBase
        {
            await Task.Delay(100);

            if (model.ServiceCallBack == null)
            {
                Console.WriteLine("not set CallBack.");
                return;
            }

            var cb = model.ServiceCallBack;

            if (cb.IsRunning)
                return;

            string callBackService = cb.callBackService.ToString();

            cb.IsRunning = true;

            string parms = "callFlowId=" + model.CallFlowid + "&" +
                           "companyId=" + model.Companyid + "&" +
                           "callMode=1" + "&" +
                           "orderId=testOrder" + "&" +
                           "conFName=远程协助系统";

            while (!cb.isStop)
            {
                WebClientPost(CallBackPost + "/" + callBackService, parms, cbHandler);
                await Task.Delay(delay);
            }

            Console.WriteLine("Stoped CallBack.");
        }

        public static void CallBackStop<T>(this T model) where T : RestAPIModelBase
        {
            if (model.ServiceCallBack != null)
            {
                model.ServiceCallBack.Stop();
            }
        }
    }
}
